package com.jike.jkgame.ui;

import java.util.HashMap;

import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.jkgame.R;
import com.jike.jkgame.data.Status;
import com.jike.jkgame.data.UserStatus;


public class UserStatusBar {
	
	private UserStatus mStatus;
	
	private HashMap<String, StatusViewHolder> mStatusViews = new HashMap<String, UserStatusBar.StatusViewHolder>();
	
	public UserStatusBar(ViewGroup parent, UserStatus status) {
		this.mStatus = status;
		ViewGroup container = (ViewGroup) parent.findViewById(R.id.health);
		StatusViewHolder holder = getViewHolder(container);
		Status property = status.getVigour();
		mStatusViews.put(property.name, holder);
		updateStatusView(property);
		
		container = (ViewGroup) parent.findViewById(R.id.ennergy);
		holder = getViewHolder(container);
		property = status.getEnergy();
		mStatusViews.put(property.name, holder);
		updateStatusView(property);
		
//		container = (ViewGroup) parent.findViewById(R.id.ammo);
//		holder = getViewHolder(container);
//		property = status.getAmmo();
//		mStatusViews.put(property.name, holder);
//		updateStatusView(property);
		
	}
	
	private StatusViewHolder getViewHolder(ViewGroup container) {
		StatusViewHolder holder = new StatusViewHolder();
		
		holder.icon = (ImageView) container.findViewById(R.id.ic_property);
		holder.value = (TextView) container.findViewById(R.id.tv_property_value);
		holder.time = (TextView) container.findViewById(R.id.tv_property_time);
		
		return holder;
	}
	
	public void updateStatusView(Status property) {
		StatusViewHolder holder = mStatusViews.get(property.name);
		holder.value.setText(property.value + "/" + property.maxValue);

		holder.time.setText(property.name);
		
	}
	
	public void updateStatusView(Status property, int seconds) {
		StatusViewHolder holder = mStatusViews.get(property.name);
		
		if (holder == null) {
			return;
		}
		
		holder.value.setText(property.value + "/" + property.maxValue);

		int minutes = seconds / 60;
		seconds = seconds % 60;

		holder.time.setText(String.format("%d:%02d", minutes, seconds));
		
	}

	
	static class StatusViewHolder  {
		ImageView icon;
		TextView value;
		TextView time;
	}

}
